/**
 * 
 */
package org.pub.app.lunch;

import org.pub.app.view.SelectAnimation;
import org.pub.lunch.google.map.view.SelectedRestaurantMap;
import org.pub.lunch.mashup.google.Restaurant;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Austin Cho
 *
 */
public class AnimationTestActivity extends Activity {
	
	public static String INTENT_SPEED = "SPEED";
	private final String TAG = "AnimationTestActivity";
	private int speed = 0;
	private boolean isAlive = true;
//	private final int ANI_DURATION_TIME = 10;
//	private ViewFlipper vwFlipper;
//	private TextSwitcher tSwitcher;
	
	
//	private int currentDurationTime = ANI_DURATION_TIME;
	
//	public AnimationTestActivity( int speed ){
//		this.speed = speed;
//	}
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		Intent in = getIntent();
		speed = (int)in.getFloatExtra( INTENT_SPEED , 0 );
		SelectAnimation sani = new SelectAnimation( this , window.getWindowManager().getDefaultDisplay().getHeight() );
//		ArrayList< Restaurant > ret = new ArrayList< Restaurant >();
//		Restaurant res = new Restaurant("봉이찜닭", "경기도 안양시 동안구", "011-9000-1111", null);
//		ret.add( res );
//		res = new Restaurant("봉이찜닭", "경기도 안양시 동안구", "011-9000-1112", null);
//		ret.add( res );
//		res = new Restaurant("봉이찜닭", "경기도 안양시 동안구", "011-9000-1113", null);
//		ret.add(res);
		sani.setItem( RestaurantList.items , speed );
		setContentView( sani );
	}
	
	
	@Override
	protected void onStop( ){
		super.onStop();
		isAlive = false;
	}
	
	@Override
	public Dialog onCreateDialog( int itemIndex ){
		final Restaurant res = RestaurantList.items.get(itemIndex);
		LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = vi.inflate(R.layout.select_item_layout, null);
		
		Builder builder = new AlertDialog.Builder( this );
		
		
		ImageView iv = (ImageView)v.findViewById( R.id.iv_thumbnail );
		TextView tvTitle = (TextView)v.findViewById( R.id.tv_title );
		TextView tvAddr = (TextView)v.findViewById( R.id.tv_addr );
		TextView tvPhone = (TextView)v.findViewById( R.id.tv_phone );
		Button btnMap = (Button) v.findViewById( R.id.btn_gps_search );
		btnMap.setOnClickListener(new OnClickListener() {
			
//			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String name = res.getName();
				Intent mapIntent = new Intent(getBaseContext(), SelectedRestaurantMap.class);
				mapIntent.putExtra("RESTAURANT", new Location(res.getLocation()));
				mapIntent.putExtra("RESTAURANT_NAME", new String(name));
				mapIntent.putExtra("CURRENT", RestaurantList.currentLocation);
				startActivity(mapIntent);
			}
		});
		
		Button btnCall = (Button) v.findViewById( R.id.btn_call );
		btnCall.setOnClickListener(new OnClickListener() {
			
//			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String phoneNumber = res.getPhone().trim().split(",", 1)[0];
				if (phoneNumber == null || phoneNumber.equals("")) {
					Toast.makeText(getBaseContext(), R.string.no_phone, 5).show();
				} else {
					phoneNumber = "tel:" + phoneNumber.replace("-", "");
					Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(phoneNumber));
					startActivity(dialIntent);
				}
			}
		});
		
		if( res.getDrawable() != null )
			iv.setImageDrawable( res.getDrawable() );
		tvTitle.setText( res.getName() );
		tvAddr.setText( res.getAddress() );
		tvPhone.setText( res.getPhone() );
		
		
		builder.setView( v );
//		iv.setBackgroundResource)
		return builder.create();
	}
	
	public Handler timeUpdateHandle = new Handler(){
		
		public void handleMessage( Message message ){
//			switch( message.what ){
//			case R.id.ms_update_timer:
			Log.w( TAG , "CALL HANDLER" );
				showDialog(message.what);
//				break;
//			}
		}
	};
	
	
//	Runnable timeCheckThread = new Runnable(){
//		public void run(){
//			try{
//				while( isAlive ){
//					Thread.yield();
//					Thread.sleep( 2000 );
//					timeUpdateHandle.sendEmptyMessage( R.id.ms_update_timer );
//					Log.w( TAG , "START ANI COUNT" );
//				}
//			}catch( Exception e ){
//				Log.w( "" , e );
//			}
//		}
//	};
}
